const router = require('koa-router')()
const basiAuth = require("basic-auth")
// 微信云开发token
const {
    getToken,
    Addurl,
    selurl
} = require('../../config/databaseapi')
const {
    showtoken
} = require("../../token/showtoken")
// 操作数据库
const result = require('../../config/result')
// 校验
const {
    regcheck,
    shoinfo
} = require("../../config/register")
// 图片上传
const {
    upload,
    confun
} = require("../../cos/index")
// after 后台服务器
router.prefix('/api/after')


//图片上传
router.post('/upload', upload.single('file'), async ctx => {
    try {
        const res = await confun(ctx.file.filename, ctx.file.path, ctx.file.size)
        console.log("上传图片成功的路径", res)
        new result(ctx, 'SUCCESS', 200, 'https://' + res).answer()
    } catch (e) {
        new result(ctx, '上传图片失败!服务器发生错误', 500).answer()
    }
})



// 获取分类内容 no
router.get('/obtainunit', async ctx => {
    const query = `db.collection('dishunit').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        console.log("错误管理", error)
    }
})

// 查询口味分类 口味 no
router.get('/capacity', async ctx => {
    const query = `db.collection('capacity').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        return error
    }
})
// 查询口味分类 容量 no
router.get('/flavor', async ctx => {
    const query = `db.collection('flavor').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        return error
    }
})

// 查询菜品分类 no
router.get('/foodtype', async ctx => {
    const query = `db.collection('food-type').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        return error
    }
})

router.post('/setspping', async ctx => {
    console.log("传过来的值")
    ctx.body = {
        title: 'sucess'
    }
})

// module.exports = router